package 二叉树算法;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

public class preorderTraversal144V2 {

    public List<Integer> preorderTraversal144(Treeroot root) {
        List<Integer> list = new ArrayList<>();

        if (root == null) {
            return list;
        }

        Deque<Treeroot> stack = new ArrayDeque<>();
        while (!stack.isEmpty() || root != null) {
            while (root != null) {
                list.add(root.val);
                stack.add(root);
                root = root.left;
            }
            root = stack.pop();
            root = root.right;
        }
        return list;
    }

    public class Treeroot {
        int val;
        Treeroot left;
        Treeroot right;

        Treeroot() {
        }

        Treeroot(int val) {
            this.val = val;
        }

        Treeroot(int val, Treeroot left, Treeroot right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}